package com.aaa.service.impl;

import com.aaa.dao.NoticeDao;
import com.aaa.entity.Notice;
import com.aaa.service.NoticeService;
import com.aaa.util.AccountName;
import com.aaa.vo.NoticeVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description: 1.0
 * @CreateTime: 2023-03-29  15:41
 * @Author: ql
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeDao noticeDao;

    @Override
    public Result<IPage<Notice>> queryPageList(NoticeVo noticeVo) {
        IPage<Notice> noticePage = new Page<>(noticeVo.getPageNum(),noticeVo.getPageSize());
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(noticeVo.getNoticeTitle())){
            wrapper.like("notice_title",noticeVo.getNoticeTitle());
        }
        if (StringUtils.hasText(noticeVo.getNoticeType())){
            wrapper.like("notice_type",noticeVo.getNoticeType());
        }
        if (StringUtils.hasText(noticeVo.getCreateBy())){
            wrapper.like("create_by",noticeVo.getCreateBy());
        }
        if (StringUtils.hasText(noticeVo.getStatus())){
            wrapper.like("status",noticeVo.getStatus());
        }
        IPage<Notice> noticeIPage = noticeDao.selectPage(noticePage, wrapper);

        return new Result<>(2000,"ok",noticeIPage);
    }

    @Override
    public Result<Notice> queryNoticeById(Long id) {
        Notice notice = noticeDao.selectById(id);
        return new Result<>(2000,"ok",notice);
    }
    @Autowired
    private AccountName accountName;
    @Override
    public Result<String> addOrUpdNotice(Notice notice) {
        String userName = accountName.getAccountName();
        System.out.println("添加或修改的====================== " + notice);
        int num;
        if (notice.getNoticeId()!=null){
            notice.setUpdateBy(userName);
            notice.setUpdateTime(new Date());
            //修改
            num = noticeDao.updateById(notice);
        }else {
            notice.setCreateBy(userName);
            notice.setCreateTime(new Date());
            num = noticeDao.insert(notice);
        }
        if (num>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result<String> deleteNoticeByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = noticeDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();

    }
}
